#include "clcomconfigfile.h"
#include "CLComConfigFileImpl.h"
#include <mutex>

static map<string, CCLComConfigFileImpl*> s_mapOBJs;
static mutex s_lock;
CLCOM_CONFIGFILE_RETURNVALUE CLComConfigFile::CreateConfigOJB(CLComConfigFile** ppObj, CLCOMCONFIGFILEINFO initInfo)
{
    std::lock_guard<mutex> lg(s_lock);

    map<string, CCLComConfigFileImpl*>::iterator it = s_mapOBJs.find(initInfo._czFilename);
    if (it == s_mapOBJs.end())
    {
        *ppObj = new CCLComConfigFileImpl(initInfo);
    }
    else
    {
        return CLCOM_CONFIGFILE_RETURNVALUE_FILEHADOPEN;
    }

    return CLCOM_CONFIGFILE_RETURNVALUE_OK;
}
CLComConfigFile* CLComConfigFile::GetConfigOJB(const char* filename)
{
    std::lock_guard<mutex> lg(s_lock);
    map<string, CCLComConfigFileImpl*>::iterator it = s_mapOBJs.find(filename);
    if (it == s_mapOBJs.end())
    {
        return NULL;
    }

    return it->second;
}
void CLComConfigFile::ReleaseConfigOBJ(CLComConfigFile** ppObj)
{
    if (*ppObj)
    {
        CCLComConfigFileImpl* p = (CCLComConfigFileImpl*)*ppObj;
        delete p;
        *ppObj = nullptr;
    }
}
